#!/bin/bash
ARCH=$(uname -m)
BUILD_SCRIPT_DIR=$(cd $(dirname "$0"); pwd)
OS_NAME="openEuler"
PRODUCTS=${OS_NAME}
OS_VERSION="24.09"
THIRD_REPO=https://eulermaker.compass-ci.openeuler.openatom.cn/api/ems5/repositories/devstation/openEuler:24.09/${ARCH}
SHA256SUM=".sha256sum"
yum_conf="${BUILD_SCRIPT_DIR}/repofile.conf"
yum clean all -c "${yum_conf}"
if rpm -q oemaker &> /dev/null; then
    yum remove oemaker -y
fi
if rpm -q lorax &> /dev/null; then
    yum remove lorax -y
fi
yum install oemaker lorax -y -c "${yum_conf}"
cd /opt/oemaker

sed -i "/^%pre/i repo --name=\"everything\" --baseurl=http://121.36.84.172/dailybuild/EBS-openEuler-24.09/rc7_openeuler-2024-09-28-08-34-02/everything/${ARCH}\nrepo --name=\"epol\" --baseurl=http://121.36.84.172/dailybuild/EBS-openEuler-24.09/rc7_openeuler-2024-09-28-08-34-02/EPOL/main/${ARCH}" config/$ARCH/livecd/livecd_$ARCH.ks

sed -i 's/part \/ --fstype="ext4" --size=40000/part \/ --fstype="ext4" --size=4000/' config/$ARCH/livecd/livecd_$ARCH.ks
sed -i '/%post/a \
touch /etc/yum.repos.d/local.repo\n\
cat << EOF > /etc/yum.repos.d/local.repo\n\
[local-repo]\n\
name=local\n\
baseurl=file:///run/initramfs/live\n\
enabled=1\n\
gpgcheck=0\n\
EOF\n\
echo "LANG=zh_CN.UTF-8" > /etc/locale.conf\n\
systemctl enable calamares\n\
' config/$ARCH/livecd/livecd_$ARCH.ks
# rpmlist中文字体
echo "\nwqy-zenhei-fonts" >> config/$ARCH/livecd/rpmlist
echo -e "\ncalamares\ncalamares-devel\ncalamares-interactiveterminal\ncalamares-libs\ncalamares-plasmalnf\nkpmcore\nkpmcore-devel" >> config/$ARCH/livecd/rpmlist
sed -i '/python3-bind/d' config/$ARCH/livecd/rpmlist
cat config/$ARCH/livecd/livecd_$ARCH.ks

REPOS=`echo "${THIRD_REPO}" | sed 's/[ \t]*$//g'`
set +e
num=0
set +u

bash -x oemaker -t livecd -p ${PRODUCTS} -v "${OS_VERSION}" -r "" -s "${REPOS}"

cd /result
livecd_iso=$(ls *.iso)
mkdir -p /mnt/iso /mnt/iso_cp
mount $livecd_iso /mnt/iso
cp -rf /mnt/iso/* /mnt/iso_cp/
umount /mnt/iso

volume_id=$(isoinfo -d -i $livecd_iso | grep "Volume id" | awk '{print $NF}')

mkdir /mnt/iso_cp/Packages
cd /mnt/iso_cp/Packages
if [ "$ARCH" == "x86_64" ]; then
    yumdownloader yum iproute dhcp openssh grub2 kernel bash coreutils grub2-pc passwd sudo xorg-* gnome-* --alldeps --resolve -c "${yum_conf}"
else
    yumdownloader yum iproute dhcp openssh grub2-efi kernel bash coreutils efibootmgr passwd sudo xorg-* gnome-* --alldeps --resolve -c "${yum_conf}"
fi
cd /result
mkdir /mnt/iso_cp/repodata
cp /opt/oemaker/config/${ARCH}/normal.xml /mnt/iso_cp/repodata/
createrepo -d -g /mnt/iso_cp/repodata/normal.xml /mnt/iso_cp
iso_name="${PRODUCTS}-${OS_VERSION}-DevStation-${ARCH}-dvd.iso"
if [ "$ARCH" == "x86_64" ]; then
    mkisofs -R -J -T -r -l -d -joliet-long -allow-multidot -allow-leading-dots -no-bak -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -eltorito-alt-boot -e images/efiboot.img -no-emul-boot -V "$volume_id" -o /result/${iso_name} /mnt/iso_cp
else
    mkisofs -R -J -T -r -l -d -joliet-long -allow-multidot -allow-leading-dots -no-bak -b EFI/BOOT/BOOTAA64.EFI -c boot.catalog -no-emul-boot -boot-load-size 4 -boot-info-table -V "$volume_id" -o /result/${iso_name} /mnt/iso_cp
fi

function create_checksum()
{
    file="$1"
    sha256sum "${file}" > "$file${SHA256SUM}"
}

set -e
cd "/result"
TGZ_NAME=$(ls ${iso_name})
if [ x"${TGZ_NAME}" == x'' ]; then  log_error "can not find iso";fi
create_checksum "${TGZ_NAME}"
iso_rpmlist="${OS_NAME}-${OS_VERSION}-DevStation-${ARCH}.rpmlist"
mkdir temp && mount *"${ARCH}"-dvd.iso temp
cd temp/Packages
ls *.rpm > "../../${iso_rpmlist}"
cd ../..
umount temp
[ -n temp ] && rm -rf temp
